package com.situ.mall.config;

import com.situ.mall.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Component <bean></bean>
 * @Controller @Service @Repository
 * 这四个作用一样
 *
 * @Configuration: 用于定义配置类，可以替换xml配置文件
 * 加了这个注解的类的内部可以包含一个或多个被@Bean注解的方法
 */

@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pic/**")
                .addResourceLocations("file:/D:/mypic/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    /*<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.situ.mvc.interceptor.MyInterceptor1"/>
		</mvc:interceptor>
	</mvc:interceptors>*/
    // 用这个方法注册拦截器，配置后才能实现
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**") // 代表拦截所有请求
                .excludePathPatterns("/user/login", "/user/getLoginPage", "/auth/code", "/static/**"); // 排除不需要拦截的
    }
}
